/**
 * Created by ACER on 2017/4/13.
 */
app.controller('others-task-details', function ($scope, $http) {
    var taskhomeid = sessionStorage.getItem('taskhomeid');

    if (!taskhomeid) {
        $.alert('任务不存在');
        location.href = '#home';
        return;
    }

    $scope.taskId = JSON.parse(taskhomeid);

    $http({
        method: 'get',
        url: app.netPath + '/tasks/detailedInfo',
        params: {taskId: $scope.taskId}

    }).then(function (res) {


        //判断任务状态
        $scope.taskInfo = res.data.data;
        if ($scope.taskInfo.taskState == 0) {
            $scope.taskInfo.taskStateWords = '待完成';
        }
        else if ($scope.taskInfo.taskState == 1) {
            $scope.taskInfo.taskStateWords = '已完成';
        }
        else if ($scope.taskInfo.taskState == 2) {
            $scope.taskInfo.taskStateWords = '过期';
        }

        //截止时间
        if ($scope.taskInfo.taskDeadlineTime) {
            $scope.taskInfo.taskdeadline = parseInt(( new Date($scope.taskInfo.taskDeadlineTime).valueOf() - new Date().getTime()) / 86400000);

        }
        //
        // console.log($scope.taskInfo.taskdeadline);
//头像图片
        var avatarImg = {};
        if (res.data.data.taskCreatorUser.userAvatarAddress) {
            avatarImg.imgAddress = app.netPath + '/resources/img/' + res.data.data.taskCreatorUser.userAvatarAddress;
            $scope.taskInfo.avatarImg = avatarImg;
        }
//任务图片

        $scope.taskInfo.imgAddresses = [];
        if (res.data.data.taskImageAddresses) {
            var taskImgs = JSON.parse(res.data.data.taskImageAddresses);
            if (taskImgs instanceof Array && taskImgs.length > 0) {
                for (var i = 0; i < taskImgs.length; i++) {
                    var img = app.netPath + '/resources/img/' + taskImgs[i];
                    if (taskImgs[i] == null)
                        continue;
                    var imgObject = {
                        imgSrc: img,
                        isLoad: false
                    };
                    $scope.taskInfo.imgAddresses.push(imgObject);
                }
            }
        }

        for (let p = 0; p < $scope.taskInfo.imgAddresses.length; p++) {
            var image = new Image();
            image.onload = function () {
                $scope.taskInfo.imgAddresses[p].isLoad = true;
                $scope.$digest();
            };
            image.src = $scope.taskInfo.imgAddresses[p].imgSrc;
        }

        //存储用户id

        $scope.getUserId = function () {
            sessionStorage.setItem('userid', $scope.userInfo.taskCreatorUser.userId);
            location.href = ''

        };

        $scope.getUserOtherId = function (otheruserid) {
            sessionStorage.setItem('otheruserid', otheruserid);
            location.href = '#others-profile';
        };
        //定位/距离

        $scope.latitude = $scope.taskInfo.taskPosition.positionX;
        $scope.longitude = $scope.taskInfo.taskPosition.positonY;
        if (parseInt(app.getGreatCircleDistance($scope.latitude, $scope.longitude)) > 1000)
            $scope.taskInfo.distanceKm = parseInt(app.getGreatCircleDistance($scope.latitude, $scope.longitude) / 1000);
        else {
            $scope.taskInfo.distancem = parseInt(app.getGreatCircleDistance($scope.latitude, $scope.longitude));
        }

    }, function () {
        $.alert('网络错误');
    })

});
/**
 * Created by ACER on 2017/4/14.
 */
